package cn.itcast.web.controller.system;


import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import cn.itcast.util.MailUtil;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/system/user",name = "用户模块")
public class UserController extends BaseController {

    //依赖注入
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    //依赖注入
    @Autowired
    private AmqpTemplate amqpTemplate;


    //查询
    @RequestMapping(value = "/list",name = "分页查询")
    //Shiro注解
    @RequiresPermissions("用户管理")
    public String list(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "6")Integer pageSize){
        //调用方法
        PageInfo byPage = userService.findByPage(pageNum, pageSize, getCompanyId());
        //存入域中
        request.setAttribute("page",byPage);
        //请求转发
        return "system/user/user-list";
    }

    //跳转新增页面
    @RequestMapping(value = "/toAdd",name = "新增页面")
    public String toAdd(){
        //查询所有部门
        List<User> list = userService.findAll(getCompanyId());
        //存入域中
        request.setAttribute("deptList",list);
        return "/system/user/user-add";

    }



    //新增
    @RequestMapping(value = "/edit",name = "新增/修改")
    public String edit(User user){

        //拼接邮箱信息
        String to = user.getEmail();
        String title = "Export货代云平台-" + getCompanyName();
        String content = StrUtil.format("{},恭喜您注册成功... <br>访问网址：http://localhost:8080<br>登录邮箱:{},密码：{}", user.getUserName(), user.getEmail(), user.getPassword());

        //分装到map集合
        Map<String, String> map = new HashMap<>();
        map.put("to",to);
        map.put("title",title);
        map.put("content",content);

        //对密码进行加密
        if (StrUtil.isNotEmpty(user.getPassword())){
            //对密码加密
            Md5Hash md5 = new Md5Hash(user.getPassword(),user.getEmail(),2);
            //修改密码
            user.setPassword(md5.toString());
        }


        //判断
        if (StrUtil.hasEmpty(user.getId())){

            //指定所属企业的名称和id
            user.setCompanyId(getCompanyId());
            user.setCompanyName(getCompanyName());
            //调用方法
            userService.save(user);
            //发送邮箱
            amqpTemplate.convertAndSend("mail.register",map);
           // MailUtil.sendMail(to,getCompanyName(),user.getEmail());
        }else{
            //调用修改方法
            userService.update(user);
        }

        //重定向到查询页面
        return "redirect:/system/user/list.do";
    }


    //跳转修改页面
    @RequestMapping(value = "/toUpdate",name = "跳转修改页面")
    public String toUpdate(String id){
        //获取方法
        User user = userService.findById(id);
        List<Dept> list = deptService.findAll(getCompanyId());

        //存入域中
        request.setAttribute("deptList",list);
        //将密码设置为null
        user.setPassword(null);
        request.setAttribute("user",user);
        //返回
        return "system/user/user-update";
    }


    //删除用户
    @RequestMapping(value = "/delete",name = "删除")
    public String delete(String id){
        userService.delete(id);
        return "redirect:/system/user/list.do";
    }

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/roleList",name = "进入角色分配页面")
    public String roleList(String id){
        //获取用户的信息
        User byId = userService.findById(id);
        //存入域中
        request.setAttribute("user",byId);

        //获取角色信息
        List<Role> role = roleService.findAll(getCompanyId());
        //存入域中
        request.setAttribute("roleList",role);

        //调用中间表的信息
        List<String> roleIdListByUserId = userService.findRoleIdListByUserId(id);

        //将已勾选角色id拼接为字符串, 格式为id,id,id
        String userRoleStr="";
        for (String roleId : roleIdListByUserId) {
            //拼接
            userRoleStr += roleId+",";
        }
        //存入域中
        request.setAttribute("userRoleStr",userRoleStr);
        //转发跳转页面
        return "system/user/user-role";
    }

    @RequestMapping(value = "/changeRole",name = "实现用户角色的分配,操作中间表")
    public String changeRole(String userId,String[] roleIds){
        //调用方法
        userService.changeRole(userId,roleIds);
        //重定向到list页面
        return "redirect:/system/user/list.do";
    }

}
